﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Jy123.Web.Manage
{
    public partial class MPage : MgConfig
    {
        protected string msgShow;
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("MPage.aspx");
            if (!IsPostBack)
            {
                if (Request.QueryString["msg"] != null)
                {
                    if (Request.QueryString["msg"] == "ok")
                    {
                        Message = MessageBox("页面添加成功！", MsgType.ok);
                    }
                }
                try
                {
                    DataTable dt = new BLL.Manage_Page().GetList("UpID = 0").Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        DataRow newRow = dt.NewRow();
                        newRow["ID"] = "0";
                        newRow["PName"] = "请选择";
                        newRow["Url"] = "#";
                        dt.Rows.InsertAt(newRow, 0);
                        dropGroup.DataSource = dt;
                        dropGroup.DataTextField = "PName";
                        dropGroup.DataValueField = "ID";
                        dropGroup.DataBind();
                    }
                    else
                    {
                        dropGroup.Items.Add(new ListItem("请添加分类", "0"));
                    }
                }
                catch
                {
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.Manage_Page mpage = new Jy123.Model.Manage_Page();
            try
            {
                if (Convert.ToInt32(dropGroup.SelectedValue) != 0)
                {
                    mpage.UpID = Convert.ToInt32(dropGroup.SelectedValue);
                }
                else
                {
                    Model.Manage_Page uppage = new Jy123.Model.Manage_Page();
                    uppage.PName = txtGroup.Value;
                    uppage.UpID = 0;
                    uppage.Url = "#";
                    mpage.UpID = new BLL.Manage_Page().Add(uppage);
                }
                mpage.PName = txtPame.Value;
                mpage.Url = txtUrl.Value;
                new BLL.Manage_Page().Add(mpage);
            }
            catch
            {
                Message = MessageBox("添加失败！", MsgType.error);
            }
            Response.Redirect(Request.Url.ToString() + "?msg=ok");
        }
        protected string GetGroupName(object upid)
        {
            Model.Manage_Page mpage = new BLL.Manage_Page().GetModel(int.Parse(upid.ToString()));
            return mpage.PName;
        }
        protected DataSet GetGroupList()
        {
            return new BLL.Manage_Page().GetList("upid=0");
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HiddenField hidGroup = (HiddenField)e.Row.FindControl("hidGroup");
                DropDownList dropGroup = (DropDownList)e.Row.FindControl("dropGroupList");
                if (dropGroup != null)
                {
                    dropGroup.DataSource = GetGroupList();
                    dropGroup.DataTextField = "PName";
                    dropGroup.DataValueField = "ID";
                    dropGroup.DataBind();
                    dropGroup.SelectedValue = hidGroup.Value;
                    hidGroupID.Value = hidGroup.Value;
                }
            }
        }

    }
}
